﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;

namespace Mengsk.ProxyDelivery.App.UI.Accounting
{
    /// <summary>
    /// SellerTradeUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class SellerPayUserControl : UserControl, IPlugin
    {
        private SellerPayService sellerPayService = ServiceContainer.GetService<SellerPayService>();

        public SellerPayUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "支付记录"; }
        }

        public Control UIControl
        {
            get { return this; }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbbPayTypes.Bind<SellerPayType>();
            this.cbbState.Bind<SellerPayState>();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.pbBar.Parameters.Clear();
                long id = 0;
                long.TryParse(this.tbId.Text.Trim(), out id);
                int sellerId = 0;
                int.TryParse(this.tbSellerId.Text.Trim(), out sellerId);
                this.pbBar.Parameters.Add("id", id);
                this.pbBar.Parameters.Add("sellerId", sellerId);
                this.pbBar.Parameters.Add("payType", this.cbbPayTypes.SelectedIndex);
                this.pbBar.Parameters.Add("state", this.cbbState.SelectedIndex);
                this.pbBar.Parameters.Add("operator", this.tbOperator.Text.Trim());
                this.pbBar.Parameters.Add("startCreateTime", this.dpStart.SelectedDate == null ? DateTime.MinValue : this.dpStart.SelectedDate.Value.Date);
                this.pbBar.Parameters.Add("endCreateTime", this.dpEnd.SelectedDate == null ? DateTime.MinValue : this.dpEnd.SelectedDate.Value.Date.AddDays(1));
                this.pbBar.StartPage();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btnMarkCompelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SellerPay st = this.dgvItems.SelectedItem as SellerPay;
                if (st == null)
                {
                    MessageBox.Show("请选择交易记录");
                    return;
                }
                new SellerTradeMarkCompleteWindow { SellerTrade = st }.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pbBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var data = this.sellerPayService.GetByAll(e.GetParameter<long>("id"), e.GetParameter<int>("sellerId"), e.GetParameter<PayTypes>("payType"), e.GetParameter<SellerPayState>("state"), "", e.GetParameter<string>("operator"), e.GetParameter<DateTime>("startCreateTime"), e.GetParameter<DateTime>("endCreateTime"), e.CurrentPage - 1, e.PageSize);
            this.dgvItems.ItemsSource = data.Datas;
            this.pbBar.Total = data.Total;
            this.pbBar.CurrentCount = data.Datas.Count;
        }

        private void btnViewDetail_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SellerPay st = this.dgvItems.SelectedItem as SellerPay;
                if (st == null)
                {
                    MessageBox.Show("请选择交易记录");
                    return;
                }
                new SellerTradeDetailWindow { Trade = st }.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
